package com.itheima.studentmanager.service.impl;

import com.itheima.studentmanager.mapper.StudentMapper;
import com.itheima.studentmanager.pojo.Student;
import com.itheima.studentmanager.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * The type Student service.
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public Student register(Student student) {
        String password = student.getPassword();

        Integer count = studentMapper.usernameExist(student.getUsername());
        if (Objects.nonNull(count)) {
            throw new RuntimeException("用户名已存在");
        }
        Integer gender = student.getGender();
        if (gender != null && gender != 1 && gender != 2) {
            throw new RuntimeException("性别不符合要求");
        }
        if (password.length() < 6 || password.length() > 16) {
            throw new RuntimeException("密码不符合规范");
        }
        studentMapper.register(student);
        return student;
    }


    @Override
    public Student getStudentById(Integer id) {

        Integer count = studentMapper.getIdExists();
        if (Objects.isNull(count)) {
            throw new RuntimeException("id不存在");
        }
        return studentMapper.getStudentById(id);
    }

    @Override
    public void deleteStudentById(Integer id) {
        studentMapper.deleteStudentById(id);
    }

    @Override
    public List<Student> findAllStudent() {
        return studentMapper.findAllStudent();
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.updateStudent(student);

    }

    @Override
    public void deleteBatchStudent(List<Integer> ids) {
        studentMapper.deleteBatchStudent(ids);
    }


}
